package com.zzyl.nursing.controller.member;

import cn.hutool.core.date.LocalDateTimeUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.zzyl.common.core.controller.BaseController;
import com.zzyl.common.core.domain.AjaxResult;
import com.zzyl.common.core.domain.BaseEntity;
import com.zzyl.common.core.domain.R;
import com.zzyl.common.core.page.TableDataInfo;
import com.zzyl.common.utils.UserThreadLocal;
import com.zzyl.nursing.domain.FamilyMember;
import com.zzyl.nursing.domain.Reservation;
import com.zzyl.nursing.service.impl.FamilyMemberServiceImpl;
import com.zzyl.nursing.service.impl.ReservationServiceImpl;
import com.zzyl.nursing.service.impl.WechatServiceImpl;
import com.zzyl.nursing.vo.health.TimeCountVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Locale;

/**
 * 预约信息Controller
 * 
 * @author ruoyi
 */
@RestController
@RequestMapping("/member/reservation")
@Api(tags =  "预约信息相关接口")
public class MemberReservationController extends BaseController
{
    @Autowired
    private FamilyMemberServiceImpl familyMemberService;

    @Autowired
    private ReservationServiceImpl reservationService;



    @GetMapping("/cancelled-count")
    @ApiOperation("查询取消预约数量")
    public R<Long> getCancelledReservationCount() {


        //根据更新人的id 查询表reservation中有多少status为2且 更新时间为当天
        Long userId = UserThreadLocal.getUserId();

        long time = System.currentTimeMillis();
        LocalDateTime ldt = LocalDateTimeUtil.of(time);
        LocalDateTime startTime = ldt.toLocalDate().atStartOfDay();
        LocalDateTime endTime = startTime.plusDays(1);


        Long count = reservationService.count(Wrappers.<Reservation>lambdaQuery()
                .eq(Reservation::getUpdateBy, userId)
                .eq(Reservation::getStatus, 2)
                .ge(Reservation::getUpdateTime, startTime)
                .le(Reservation::getUpdateTime, endTime));

        return R.ok(count);
    }

    @GetMapping("/countByTime")
    public R getCount(Long time)
    {
        List<TimeCountVo> list = reservationService.getCountByTime(time);
        return R.ok(list);
    }


    /**
     * 添加预约
     * @param reservation
     * @return
     */
    @PostMapping()
    public AjaxResult insertReservation(@RequestBody Reservation reservation)
    {

        reservationService.insertReservation(reservation);
        AjaxResult aja = AjaxResult.success("成功", 200);
        return aja ;
    }


    /**
     * 获取预约列表
     * @param pageSize
     * @param pageNum
     * @param status
     * @return
     */
    @GetMapping("/page")
    @ApiOperation("分页查询")
    public R Page(Integer pageSize,Integer pageNum,Integer status)
    {

        TableDataInfo<Reservation> tabledatainfo = reservationService.getpage(pageSize, pageNum, status);
        return R.ok(tabledatainfo) ;
    }


    @PutMapping("/{id}/cancel")
    public R cancelReservation(@PathVariable Long id)
    {
        reservationService.update(Wrappers.<Reservation>lambdaUpdate()
                .eq(Reservation::getId,id)
                .set(Reservation::getStatus,2)
                .set(Reservation::getUpdateBy,UserThreadLocal.getUserId())
                .set(Reservation::getUpdateTime,LocalDateTimeUtil.now()));
        return R.ok();
    }


}